﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZGEDrySaltery.Code;

namespace ZGEDrySaltery.Web.Areas.SystemManage.Controllers
{
    public class BaseController : Controller
    {
        [AcceptVerbs(HttpVerbs.Post)]
        public JsonResult Upload(HttpPostedFileBase fileData)
        {
            if (fileData != null)
            {
                try
                {
                    // 文件上传后的保存路径
                    string filename = DateTime.Now.ToString("yyyyMMddHHmmssfff") + (new Random()).Next().ToString().Substring(0, 4) + ".jpg";//图片名称
                    // img-path images路径--Start
                    string filePathDate = DateTime.Now.ToShortDateString().ToString();
                    filePathDate = filePathDate.Replace("-", "/");
                    // img-path images路径--End
                    string imagePath = "/image/" + "/" + filePathDate + "/";
                    string filepath = Server.MapPath("~/upload") + imagePath;
                    if (!Directory.Exists(filepath))
                    {
                        Directory.CreateDirectory(filepath);
                    }
                    fileData.SaveAs(filepath + filename);
                    string imageurl = Configs.GetValue("ImageUrl");
                    return Json(new { Success = true, status = 1, FileName = filename, path = imagePath, imageurl = imageurl }, JsonRequestBehavior.AllowGet);

                }
                catch (Exception ex)
                {
                    return Json(new { Success = false, Message = ex.Message }, JsonRequestBehavior.AllowGet);
                }
            }
            else
            {

                return Json(new { Success = false, Message = "请选择要上传的文件！" }, JsonRequestBehavior.AllowGet);
            }
        }
    }
}